add more convenience API.
authorKristian Rietveld <kris@imendio.com>
Thu, 19 Jul 2007 13:21:09 +0000 (13:21 +0000)
committerKristian Rietveld <kristian@src.gnome.org>
Thu, 19 Jul 2007 13:21:09 +0000 (13:21 +0000)
2007-07-19  Kristian Rietveld  <kris@imendio.com>

* gtk/gtk.symbols:
* gtk/gtktreeprivate.h:
* gtk/gtktreeview.[ch] (gtk_tree_view_get_tooltip_context),
(gtk_tree_view_[sg]et_tooltip_column): add more convenience API.

* tests/testtooltip.c (query_tooltip_tree_view_cb): use
gtk_tree_view_get_tooltip_context().

* demos/gtk-demo/demo.ui: add a tooltip column to the list store,
set tooltip-column on the tree view.

svn path=/trunk/; revision=18496

ChangeLog
demos/gtk-demo/demo.ui
gtk/gtk.symbols
gtk/gtktreeprivate.h
gtk/gtktreeview.c
gtk/gtktreeview.h
tests/testtooltips.c

index 64a4b9d81749a2ede8c3f47dc41c041381c8e8a2..fcf3e1af719f6be138ce8a8f3c9442e18f4faddc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2007-07-19  Kristian Rietveld  <kris@imendio.com>
+
+       * gtk/gtk.symbols:
+       * gtk/gtktreeprivate.h:
+       * gtk/gtktreeview.[ch] (gtk_tree_view_get_tooltip_context),
+       (gtk_tree_view_[sg]et_tooltip_column): add more convenience API.
+
+       * tests/testtooltip.c (query_tooltip_tree_view_cb): use
+       gtk_tree_view_get_tooltip_context().
+
+       * demos/gtk-demo/demo.ui: add a tooltip column to the list store,
+       set tooltip-column on the tree view.
+
 2007-07-18  Richard Hult  <richard@imendio.com>
 
        * gdk/quartz/gdkcursor-quartz.c (gdk_cursor_new_for_display): Ref
index 1c84db2b581da5aa6e4d4cb2efb2bc9dc4f85e60..a877223f0dcf850d59a189c8f0d69420e00ecd08 100644 (file)
@@ -5,17 +5,20 @@
         <column type="gchararray"/>
         <column type="gchararray"/>
         <column type="gint"/>
+        <column type="gchararray"/>
       </columns>
       <data>
         <row>
           <col id="0">John</col>
           <col id="1">Doe</col>
           <col id="2">25</col>
+          <col id="3">This is the John Doe row</col>
         </row>
         <row>
           <col id="0">Mary</col>
           <col id="1">Dole</col>
           <col id="2">50</col>
+          <col id="3">This is the Mary Dole row</col>
         </row>
       </data>
     </object>
                         <object class="GtkTreeView" id="treeview1">
                           <property name="visible">True</property>
                           <property name="model">liststore1</property>
+                          <property name="tooltip-column">3</property>
                           <child>
                             <object class="GtkTreeViewColumn" id="column1">
                               <property name="title">Name</property>
index a993db7767aad7d7d3a848e27a1e18263f215f53..c24a60536dee7fcaeb9bf030c684df45eb0d8180 100644 (file)
@@ -4426,6 +4426,8 @@ gtk_tree_view_get_search_equal_func
 gtk_tree_view_get_search_position_func
 gtk_tree_view_get_selection
 gtk_tree_view_get_show_expanders
+gtk_tree_view_get_tooltip_column
+gtk_tree_view_get_tooltip_context
 gtk_tree_view_get_type G_GNUC_CONST
 gtk_tree_view_get_vadjustment
 gtk_tree_view_get_visible_range
@@ -4471,6 +4473,7 @@ gtk_tree_view_set_search_position_func
 gtk_tree_view_set_show_expanders
 gtk_tree_view_set_tooltip_row
 gtk_tree_view_set_tooltip_cell
+gtk_tree_view_set_tooltip_column
 gtk_tree_view_set_vadjustment
 #ifndef GTK_DISABLE_DEPRECATED
 gtk_tree_view_tree_to_widget_coords
index 696005828a1ce305a364f82a27f85a7e2989abbd..9d49f394df0cff8a8ae55ed6ee2fd23222e10025 100644 (file)
@@ -264,6 +264,8 @@ struct _GtkTreeViewPrivate
 
   gboolean tree_lines_enabled;
   GdkGC *tree_line_gc;
+
+  gint tooltip_column;
 };
 
 #ifdef __GNUC__
index e0591a4f489339f7958b1fe0676b66b8c7af0366..dbf25fa2c854039275b87d8d4903e5d85da2e4cb 100644 (file)
@@ -143,7 +143,8 @@ enum {
   PROP_LEVEL_INDENTATION,
   PROP_RUBBER_BANDING,
   PROP_ENABLE_GRID_LINES,
-  PROP_ENABLE_TREE_LINES
+  PROP_ENABLE_TREE_LINES,
+  PROP_TOOLTIP_COLUMN
 };
 
 /* object signals */
@@ -756,6 +757,16 @@ gtk_tree_view_class_init (GtkTreeViewClass *class)
                                                            FALSE,
                                                            GTK_PARAM_READWRITE));
 
+    g_object_class_install_property (o_class,
+                                    PROP_TOOLTIP_COLUMN,
+                                    g_param_spec_int ("tooltip-column",
+                                                      P_("Tooltip Column"),
+                                                      P_("The column in the model containing the tooltip texts for the rows"),
+                                                      -1,
+                                                      G_MAXINT,
+                                                      -1,
+                                                      GTK_PARAM_READWRITE));
+
   /* Style properties */
 #define _TREE_VIEW_EXPANDER_SIZE 12
 #define _TREE_VIEW_VERTICAL_SEPARATOR 2
@@ -1349,6 +1360,8 @@ gtk_tree_view_init (GtkTreeView *tree_view)
 
   tree_view->priv->grid_lines = GTK_TREE_VIEW_GRID_LINES_NONE;
   tree_view->priv->tree_lines_enabled = FALSE;
+
+  tree_view->priv->tooltip_column = -1;
 }
 
 \f
@@ -1422,6 +1435,9 @@ gtk_tree_view_set_property (GObject         *object,
     case PROP_ENABLE_TREE_LINES:
       gtk_tree_view_set_enable_tree_lines (tree_view, g_value_get_boolean (value));
       break;
+    case PROP_TOOLTIP_COLUMN:
+      gtk_tree_view_set_tooltip_column (tree_view, g_value_get_int (value));
+      break;
     default:
       break;
     }
@@ -1493,6 +1509,9 @@ gtk_tree_view_get_property (GObject    *object,
     case PROP_ENABLE_TREE_LINES:
       g_value_set_boolean (value, tree_view->priv->tree_lines_enabled);
       break;
+    case PROP_TOOLTIP_COLUMN:
+      g_value_set_boolean (value, tree_view->priv->tooltip_column);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -15279,5 +15298,173 @@ gtk_tree_view_set_tooltip_cell (GtkTreeView       *tree_view,
   gtk_tooltip_set_tip_area (tooltip, &rect);
 }
 
+/**
+ * gtk_tree_view_get_tooltip_contenxt:
+ * @tree_view: a #GtkTreeView
+ * @x: the x coordinate (relative to widget coordinates)
+ * @y: the y coordinate (relative to widget coordinates)
+ * @keyboard_tip: whether this is a keyboard tooltip or not
+ * @model: a pointer to receive a #GtkTreeModel or %NULL
+ * @path: a pointer to receive a #GtkTreePath or %NULL
+ * @iter: a pointer to receive a #GtkTreeIter or %NULL
+ *
+ * This function is supposed to be used in a GtkWidget::query-tooltip
+ * signal handler for #GtkTreeViews.  The @x, @y and @keyboard_tip values
+ * which are received in the signal handler, should be passed to this
+ * function without modification.
+ *
+ * The return value indicates whether there is a tree view row at the given
+ * coordinates (%TRUE) or not (%FALSE) for mouse tooltips.  For keyboard
+ * tooltips the row returned will be the cursor row.  When %TRUE, then any of
+ * @model, @path and @iter which have been provided will be set to point to
+ * that row and the corresponding model.  @x and @y will always be converted
+ * to be relative to @tree_view's bin_window if @keyboard_tooltip is %FALSE.
+ *
+ * Return value: whether or not the given tooltip context points to a row.
+ *
+ * Since: 2.12
+ */
+gboolean
+gtk_tree_view_get_tooltip_context (GtkTreeView   *tree_view,
+                                  gint          *x,
+                                  gint          *y,
+                                  gboolean       keyboard_tip,
+                                  GtkTreeModel **model,
+                                  GtkTreePath  **path,
+                                  GtkTreeIter   *iter)
+{
+  GtkTreePath *tmppath = NULL;
+
+  g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), FALSE);
+  g_return_val_if_fail (x != NULL, FALSE);
+  g_return_val_if_fail (y != NULL, FALSE);
+
+  if (keyboard_tip)
+    {
+      gtk_tree_view_get_cursor (tree_view, &tmppath, NULL);
+
+      if (!tmppath)
+       return FALSE;
+    }
+  else
+    {
+      gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, *x, *y,
+                                                        x, y);
+
+      if (!gtk_tree_view_get_path_at_pos (tree_view, *x, *y,
+                                         &tmppath, NULL, NULL, NULL))
+       return FALSE;
+    }
+
+  if (model)
+    *model = gtk_tree_view_get_model (tree_view);
+
+  if (iter)
+    gtk_tree_model_get_iter (gtk_tree_view_get_model (tree_view),
+                            iter, tmppath);
+
+  if (path)
+    *path = tmppath;
+  else
+    gtk_tree_path_free (tmppath);
+
+  return TRUE;
+}
+
+static gboolean
+gtk_tree_view_set_tooltip_query_cb (GtkWidget  *widget,
+                                   gint        x,
+                                   gint        y,
+                                   gboolean    keyboard_tip,
+                                   GtkTooltip *tooltip,
+                                   gpointer    data)
+{
+  gchar *str;
+  GtkTreeIter iter;
+  GtkTreePath *path;
+  GtkTreeModel *model;
+  GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
+
+  if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
+                                         &x, &y,
+                                         keyboard_tip,
+                                         &model, &path, &iter))
+    return FALSE;
+
+  gtk_tree_model_get (model, &iter, tree_view->priv->tooltip_column, &str, -1);
+
+  if (!str)
+    {
+      gtk_tree_path_free (path);
+      return FALSE;
+    }
+
+  gtk_tooltip_set_markup (tooltip, str);
+  gtk_tree_view_set_tooltip_row (tree_view, tooltip, path);
+
+  gtk_tree_path_free (path);
+  g_free (str);
+
+  return TRUE;
+}
+
+/**
+ * gtk_tree_view_set_tooltip_column:
+ * @tree_view: a #GtkTreeView
+ * @column: an integer, which is a valid column number for @tree_view's model
+ *
+ * If you only plan to have simple (text-only) tooltips on full rows, you
+ * can use this function to have #GtkTreeView handle these automatically
+ * for you.  @column should be set to the column in @tree_view's model
+ * containing the tooltip texts, or %-1 to disable this feature.
+ *
+ * When enabled, GtkWidget::has-tooltip will be set to %TRUE and
+ * @tree_view will connect a query-tooltip signal handler.
+ *
+ * Since: 2.12
+ */
+void
+gtk_tree_view_set_tooltip_column (GtkTreeView *tree_view,
+                                 gint         column)
+{
+  g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
+
+  if (column == -1)
+    {
+      g_signal_handlers_disconnect_by_func (tree_view,
+                                           gtk_tree_view_set_tooltip_query_cb,
+                                           NULL);
+      gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), FALSE);
+    }
+  else
+    {
+      g_signal_connect (tree_view, "query-tooltip",
+                       G_CALLBACK (gtk_tree_view_set_tooltip_query_cb), NULL);
+      gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), TRUE);
+    }
+
+  tree_view->priv->tooltip_column = column;
+}
+
+/**
+ * gtk_tree_view_get_tooltip_column:
+ * @tree_view: a #GtkTreeView
+ *
+ * Returns the column of @tree_view's model which is being used for
+ * displaying tooltips on @tree_view's rows.
+ *
+ * Return value: a #gint with the tooltip column that is currently being
+ * used, or %-1 if this is disabled.
+ *
+ * Since 2.12
+ */
+gint
+gtk_tree_view_get_tooltip_column (GtkTreeView *tree_view)
+{
+  g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), 0);
+
+  return tree_view->priv->tooltip_column;
+}
+
 #define __GTK_TREE_VIEW_C__
 #include "gtkaliasdef.c"
index 08180813675983282c8bae67a591a5f7763d72c0..9ea61a9ff01c91dc6db4aa9348a654aaf5403243 100644 (file)
@@ -411,6 +411,16 @@ void          gtk_tree_view_set_tooltip_cell   (GtkTreeView       *tree_view,
                                                GtkTreePath       *path,
                                                GtkTreeViewColumn *column,
                                                GtkCellRenderer   *cell);
+gboolean      gtk_tree_view_get_tooltip_context(GtkTreeView       *tree_view,
+                                               gint              *x,
+                                               gint              *y,
+                                               gboolean           keyboard_tip,
+                                               GtkTreeModel     **model,
+                                               GtkTreePath      **path,
+                                               GtkTreeIter       *iter);
+void          gtk_tree_view_set_tooltip_column (GtkTreeView       *tree_view,
+                                               gint               column);
+gint          gtk_tree_view_get_tooltip_column (GtkTreeView       *tree_view);
 
 G_END_DECLS
 
index 22f54da6c9975096e077c434fe2b30c65339956d..7aa8f59c7010764e4c5f0a0da0a6e8ca7d843d76 100644 (file)
@@ -128,7 +128,11 @@ query_tooltip_tree_view_cb (GtkWidget  *widget,
         return FALSE;
     }
 
-  gtk_tree_model_get_iter (model, &iter, path);
+  if (!gtk_tree_view_get_tooltip_context (tree_view, &x, &y,
+                                         keyboard_tip,
+                                         &model, &path, &iter))
+    return FALSE;
+
   gtk_tree_model_get (model, &iter, 0, &tmp, -1);
   pathstring = gtk_tree_path_to_string (path);